package com.xinqizhi.service.impl;

import com.xinqizhi.entity.SysDict;
import com.xinqizhi.mapper.SysDictMapper;
import com.xinqizhi.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysDictServiceImpl implements SysDictService {
    @Autowired
    private SysDictMapper dictMapper;
    /**
     * 查询所有字典列表信息
     *
     * @return
     */
    @Override
    public List<SysDict> getAllData() {
        return dictMapper.getAllData();
    }

    /**
     * 将字典根据typeName字段转存为Hash结构
     * key : typeName
     * list : typeName=key的list集合
     *
     * @return
     */
    public Map<String,List> dictList2HashByTypeName() {
        Map<String,List> hashMap = new HashMap<>();
        List<SysDict> sysDicts = this.getAllData();
        for (SysDict s : sysDicts){
            if (s.getValue().equals(s.getParentvalue())){//value==pvalue说明是父节点
                List<SysDict> list = new ArrayList<>();
                for (SysDict s1:sysDicts){
                    if (s1.getParentvalue().equals(s.getDictid().toString())){
                        list.add(s1);
                    }
                }
                hashMap.put(s.getTypename(),list);
            }
        }
        return hashMap;
    }


}
